package cn.edu.kzjsxy.graduationproject.controller;


import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.entity.Dormitories;
import cn.edu.kzjsxy.graduationproject.entity.Units;
import cn.edu.kzjsxy.graduationproject.entity.Users;
import cn.edu.kzjsxy.graduationproject.service.IDormitoriesService;
import cn.edu.kzjsxy.graduationproject.service.IUnitsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 单元信息表 前端控制器
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Api(tags = "单元信息表")
@RestController
@RequestMapping("/units")
public class UnitsController {

//	单元的service
	@Autowired
	private IUnitsService unitsService;

//	宿舍的service
	@Autowired
	private IDormitoriesService dormitoriesService;

	@GetMapping("/{unitId}")
	@ApiOperation("根据单元ID查询单元信息")
	public Result<Units> getUnitsById(@PathVariable String unitId) {
		Units units = unitsService.getById(unitId);
		units = unitsService.getBuildingsName(units);
		return Result.success(units);
	}



	@ApiOperation("添加单元信息")
	@PostMapping("/add")
	public Result addUnit(@RequestBody Units unit) {
		boolean success = unitsService.save(unit);
		return Result.judgment(success);
	}

	@ApiOperation("删除单元信息")
	@DeleteMapping("/delete/{unitId}")
	public Result deleteUnit(@PathVariable String unitId) {
		List<Dormitories> dormitoriesList = dormitoriesService.list(new LambdaQueryWrapper<Dormitories>().eq(Dormitories::getUnitId, unitId));
		if(dormitoriesList!=null&&!dormitoriesList.isEmpty()){
			return Result.error("当前单元存在宿舍信息，无法删除!");
		}
		boolean success = unitsService.removeById(unitId);
		return Result.judgment(success);
	}

	@ApiOperation("更新单元信息")
	@PutMapping("/update")
	public Result updateUnit(@RequestBody Units unit) {
		boolean success = unitsService.updateById(unit);
		return Result.judgment(success);
	}

	@ApiOperation("获取所有单元信息")
	@PostMapping("/list")
	public Result<List<Units>> selectUnitsByCondition(@RequestBody Units unit) {
		List<Units> unitsList = unitsService.selectUnitsByCondition(unit);
		return Result.success(unitsList);
	}

	@ApiOperation("分页获取单元信息 多表查询")
	@PostMapping("/page")
	public Result<PageInfo<Units>> getUnitsByPage(Page page, @RequestBody Units unit, HttpServletRequest request){
		return unitsService.selectUnitsByPage(page, unit,request);
	}


}
